<?php
/**
 * Description of User
 *
 * @author mie
 */
class Model_UserManagement extends Core_Model
{
    /**
     * Get list of user groups
     *
     * @return $listOfGroups An array of array('group' => id, 'text' => name)
     */
    public static function getGroups()
    {
        $db = self::getDatabaseHandler();
        $db->prepare('SELECT id, name FROM UserGroup ORDER BY id');
        $db->execute();

        $listOfGroups = array();
        while ($db->fetch())
        {
            $listOfGroups[] = array
            (
                'userGroupId' => $db->getResult('id'),
                'text' => $db->getResult('name')
            );
        }
        return $listOfGroups;
    }

    /**
     * Get list of user statuses
     *
     * @return $listOfGroups An array of array('status' => id, 'text' => status)
     */
    public static function getStatuses()
    {
        $db = self::getDatabaseHandler();
        $db->prepare('SELECT id, status FROM UserStatus');
        $db->execute();

        $listOfStatuses = array();
        while ($db->fetch())
        {
            $listOfStatuses[] = array
            (
                'userStatusId' => $db->getResult('id'),
                'text' => $db->getResult('status')
            );
        }
        return $listOfStatuses;
    }

    /**
     * Get list of users
     *
     * @return $listOfUsers An array of array('id' => id, 'username' => username, email => email ...)
     */
    public static function getUsers()
    {
        $db = self::getDatabaseHandler();
        $db->prepare('
            SELECT
                id,
                username,
                email,
                userGroupId,
                userStatusId
            FROM
                User
            WHERE
                userStatusId != 3
            ORDER BY username');
        $db->execute();

        $listOfUsers = array();
        while ($db->fetch())
        {
            $listOfUsers[] = array(
                'id' => $db->getResult('id'),
                'username' => $db->getResult('username'),
                'email' => $db->getResult('email'),
                'userGroupId' => $db->getResult('userGroupId'),
                'userStatusId' => $db->getResult('userStatusId')
            );
        }
        return $listOfUsers;
    }

    /**
     * Get list of users that a ticket can be assigned to (status = active)
     * @return Array
     * @throws Core_Database_Exception on database exception
     */
    public function getAssignees()
    {
        $db = self::getDatabaseHandler();
        $db->prepare('SELECT 
            id as userId, username
            FROM User
            WHERE userStatusId = 1
            ORDER BY username');
        $db->execute();
        $listOfUsers = array();
        while($db->fetch())
        {
            $listOfUsers[] = $db->getResult();
        }
        return $listOfUsers;
    }
}